package com.example.pingze.controller;

import com.example.pingze.entity.Creation;
import com.example.pingze.entity.CreationUserLove;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.service.CreationService;
import com.example.pingze.service.CreationUserLoveService;
import com.example.pingze.utils.ResultVOUtils;
import com.example.pingze.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.UUID;

@RestController
@RequestMapping("/creationUserLove")
@CrossOrigin(origins = "*")
public class CreationUserLoveController {
    @Autowired
    private CreationUserLoveService service;
    @Autowired
    private CreationService creationService;
    //添加喜欢
    @PostMapping("/love")
    public Result<String> love(@RequestParam String account,
                               @RequestParam String creationId){
        if (creationId == null){
            throw new PingZeException("收藏评论id为空");
        }
        if(account == null){
            throw new PingZeException("账号为空");
        }
        //在收藏数据之前，判断数据库中是否存在相同的数据，如果有则给出提示
        CreationUserLove love1 = service.findCreationUserLoveByAccountAndCreationId(account, creationId);
        if (love1 == null){
            CreationUserLove creationUserLove = new CreationUserLove();
            creationUserLove.setId(UUID.randomUUID().toString());
            creationUserLove.setAccount(account);
            creationUserLove.setCreationId(creationId);
            service.add(creationUserLove);
            Creation find = creationService.findCreationById(creationId);
            if(find != null){
                int love = find.getLove();
                int count = love + 1;
                find.setLove(count);
                creationService.saveCount(find);
            }
            return ResultVOUtils.result(find.getLove());
        }else {
            throw new PingZeException("已经将该评论添加收藏，无需二次收藏");
        }
    }
    //取消喜欢
    @PostMapping("/noLove")
    public Result<String> noLove(@RequestParam String account,
                                 @RequestParam String creationId){
        if (creationId == null){
            throw new PingZeException("收藏评论id为空");
        }
        if(account == null){
            throw new PingZeException("账号为空");
        }

        CreationUserLove creationUserLove = service.findCreationUserLoveByAccountAndCreationId(account, creationId);
        if (creationUserLove == null) {
            throw new PingZeException("该收藏信息不在收藏列表中");
        }
        Creation creation = creationService.findCreationById(creationId);
        if (creation != null) {
            int i = creation.getLove();
            int num = i - 1;
            creation.setLove(num);
            creationService.saveCount(creation);
            service.delete(creationUserLove);
            return ResultVOUtils.result(num);
        }else {
            throw new PingZeException("请先对该评论进行收藏");
        }
    }
    //获取love表中的状态
    @PostMapping("/getStatus")
    public Result<String> getStatus(@RequestParam String account,
                                    @RequestParam String creationId){
        String status = "";
        if(account == null){
            throw new PingZeException("用户账号为空");
        }
        if(creationId == null){
            throw new PingZeException("评论ID为空");
        }
        //通过多条件查询，将该用户收藏数据查询出来（正常情况下只能查出一条数据）
        CreationUserLove find = service.findCreationUserLoveByAccountAndCreationId(account, creationId);
        if(find != null){
            status = "ok";
        }else{
            status = "no";
        }
        return ResultVOUtils.success(status);
    }



    @CrossOrigin
    @PostMapping("/page")
    public Result<Page<CreationUserLove>> getList(
            CreationUserLove creationUserLove,
            @RequestParam(name = "pageCode",required = false,defaultValue = "1") Integer pageCode,
            @RequestParam(name = "pageSize",required = false, defaultValue = "2") Integer pageSize
    ){
        if(pageCode < 1){
            throw new PingZeException("页码不能小于1");
        }
        Sort.Order accountOrder = Sort.Order.asc("account");
        Sort sort = Sort.by(accountOrder);
        Pageable pageable = PageRequest.of(pageCode - 1, pageSize, sort);
        Page page = service.getList(creationUserLove, pageable);
        return ResultVOUtils.result(page);
    }

    @CrossOrigin
    @PostMapping("/add")
    public Result<CreationUserLove> add(String account,String creationId){
        CreationUserLove add = service.add(account,creationId);
        return ResultVOUtils.result(add);
    }

    @CrossOrigin
    @PostMapping("/edit")
    public Result<CreationUserLove> edit(String id,String account,String creationId){
        CreationUserLove edit = service.edit(id, account, creationId);
        return ResultVOUtils.result(edit);
    }
    @CrossOrigin
    @Transactional
    @PostMapping("/delete")
    public Result<String> delete(String id){
        if(id == null) {
            throw new PingZeException("id为空");
        }
        CreationUserLove find = service.findCreationUserLoveById(id);
        if(find != null){
            service.delete(find);
        }
        return ResultVOUtils.success("delete success!");
    }
}
